﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using whatpricemyhouse.LINQ;

namespace whatpricemyhouse.agent
{
    public partial class EditBuyer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            new AdminBasicPage();
            if (User.Identity.IsAuthenticated)
            {

                ImageButton ImageButton1 = this.LoginView2.FindControl("ImageButton1") as ImageButton;
                ImageButton ImageButton4 = this.LoginView2.FindControl("ImageButton4") as ImageButton;
                ImageButton ImageButton3 = this.LoginView2.FindControl("ImageButton3") as ImageButton;

                ImageButton1.Attributes.Add("onmouseover", "mouseOver1(this);");
                ImageButton1.Attributes.Add("onmouseout", "mouseOut1(this);");
                ImageButton4.Attributes.Add("onmouseover", "mouseOver2(this);");
                ImageButton4.Attributes.Add("onmouseout", "mouseOut2(this);");
                ImageButton4.Attributes.Add("OnClick", "window.print()");
                ImageButton3.Attributes.Add("onmouseover", "mouseOver3(this);");
                ImageButton3.Attributes.Add("onmouseout", "mouseOut3(this);");

                if (!IsPostBack)
                {
                    Label PostcodeLabel = this.LoginView2.FindControl("PostcodeLabel") as Label;
                    if (PostcodeLabel != null)
                    {
                        String buyerId = Request.QueryString["buyerID"];
                        if (!String.IsNullOrEmpty(buyerId)) {
                            loadData(buyerId);
                        }
                    }
                }
            }
        }

        private void loadData(String buyerId)
        {
            Label PostcodeLabel = this.LoginView2.FindControl("PostcodeLabel") as Label;
            Label PlaceLabel = this.LoginView2.FindControl("PlaceLabel") as Label;
            Label CityLabel = this.LoginView2.FindControl("CityLabel") as Label;
            HiddenField customerID = this.LoginView2.FindControl("customerID") as HiddenField;
            TextBox nameTxt = this.LoginView2.FindControl("nameTxt") as TextBox;
            TextBox emailTxt = this.LoginView2.FindControl("emailTxt") as TextBox;

            Label PhoneLabel = this.LoginView2.FindControl("PhoneLabel") as Label;
            Label PriceFromLabel = this.LoginView2.FindControl("PriceFromLabel") as Label;
            Label PriceToLabel = this.LoginView2.FindControl("PriceToLabel") as Label;
            Label PropertyTypeLabel = this.LoginView2.FindControl("PropertyTypeLabel") as Label;
            Label WhenToBuyLabel = this.LoginView2.FindControl("WhenToBuyLabel") as Label;
            Label DateReceivedLabel = this.LoginView2.FindControl("DateReceivedLabel") as Label;
            ImageButton ImageButton3 = this.LoginView2.FindControl("ImageButton3") as ImageButton;

            ImageButton3.PostBackUrl = "~/Agent/RespondToLead.aspx?buyerId=" + buyerId;

            DataControllerDataContext dcdc = new DataControllerDataContext();
            var buyers = from b in dcdc.buy_request_tables
                         where b.BRID == buyerId
                         select b;
            foreach (var buyer in buyers) {
                PostcodeLabel.Text = buyer.suburb.postcode;
                PlaceLabel.Text = buyer.suburb.suburb1;
                CityLabel.Text = buyer.suburb.area;
                customerID.Value = buyer.customer_table.CID;
                nameTxt.Text = buyer.customer_table.CName;
                emailTxt.Text = buyer.customer_table.CEmail;
                PhoneLabel.Text = buyer.customer_table.CPhone;
                var price1 = (from p in dcdc.price_range_tables
                              where p.PRID == buyer.price_range_from
                              select p.PRName).Single<String>();
                PriceFromLabel.Text = price1;
                var price2 = (from p in dcdc.price_range_tables
                              where p.PRID == buyer.price_range_to
                              select p.PRName).Single<String>();
                PriceToLabel.Text = price2;
                PropertyTypeLabel.Text = buyer.property_type_table.PTName;
                WhenToBuyLabel.Text = buyer.when_to_buy.WTBName;
                DateReceivedLabel.Text = buyer.createdDate.Value.ToString("dd/MM/yyyy");
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            HiddenField customerID = this.LoginView2.FindControl("customerID") as HiddenField;
            TextBox nameTxt = this.LoginView2.FindControl("nameTxt") as TextBox;
            TextBox emailTxt = this.LoginView2.FindControl("emailTxt") as TextBox;
            if (!String.IsNullOrEmpty(customerID.Value)) {
                DataControllerDataContext dcdc = new DataControllerDataContext();
                var custoemrs = from c in dcdc.customer_tables
                                where c.CID == customerID.Value
                                select c;
                foreach (var customer in custoemrs)
                {
                    customer.CName = nameTxt.Text;
                    customer.CEmail = emailTxt.Text;
                    dcdc.SubmitChanges();
                    break;
                }
            }
        }
    }
}